package cn.lingyangwl.agile.pay.pay.service;

import cn.lingyangwl.agile.pay.common.constants.Cs;
import cn.lingyangwl.agile.pay.common.service.MchAppService;
import cn.lingyangwl.agile.pay.common.service.MchInfoService;
import cn.lingyangwl.agile.pay.common.service.PayInterfaceConfigService;
import cn.lingyangwl.agile.pay.common.service.impl.PaySysConfigServiceImpl;
import cn.lingyangwl.agile.pay.pay.model.bo.IClientWrapper;
import cn.lingyangwl.agile.pay.pay.model.bo.IsvConfigContext;
import cn.lingyangwl.agile.pay.pay.model.bo.MchAppConfigContext;
import cn.lingyangwl.agile.pay.pay.model.params.IsvParams;
import cn.lingyangwl.agile.pay.pay.model.params.IsvsubMchParams;
import cn.lingyangwl.agile.pay.pay.model.params.NormalMchParams;
import cn.lingyangwl.agile.pay.constant.IfCodeEnum;
import cn.lingyangwl.agile.pay.model.entity.MchApp;
import cn.lingyangwl.agile.pay.model.entity.MchInfo;
import cn.lingyangwl.agile.pay.model.entity.PayInterfaceConfig;
import cn.lingyangwl.agile.pay.pay.factory.AbstractPayFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 配置信息查询服务 （兼容 缓存 和 直接查询方式）
 */
@Slf4j
@Service
public class ConfigContextQueryService {
    @Resource
    private ConfigContextService configContextService;
    @Resource
    private MchInfoService mchInfoService;
    @Resource
    private MchAppService mchAppService;
    @Resource
    private PayInterfaceConfigService payInterfaceConfigService;

    private boolean isCache() {
        return PaySysConfigServiceImpl.IS_USE_CACHE;
    }

    public MchApp queryMchApp(String mchNo, String mchAppId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId).getMchApp();
        }

        return mchAppService.getByMch(mchNo, mchAppId);
    }

    public MchAppConfigContext queryMchInfoAndAppInfo(String mchNo, String mchAppId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId);
        }

        MchInfo mchInfo = mchInfoService.getById(mchNo);
        MchApp mchApp = queryMchApp(mchNo, mchAppId);

        if (mchInfo == null || mchApp == null) {
            return null;
        }

        MchAppConfigContext result = new MchAppConfigContext();
        result.setMchInfo(mchInfo);
        result.setMchNo(mchNo);
        result.setMchType(mchInfo.getType());

        result.setMchApp(mchApp);
        result.setAppId(mchAppId);

        return result;
    }


    public NormalMchParams queryNormalMchParams(String mchNo, String mchAppId, IfCodeEnum ifCodeEnum) {
        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId).getNormalMchParamsByIfCode(ifCodeEnum);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getOne(PayInterfaceConfig.lqw()
                .select(PayInterfaceConfig::getIfCode, PayInterfaceConfig::getIfParams)
                .eq(PayInterfaceConfig::getStatus, Cs.YES)
                .eq(PayInterfaceConfig::getInfoType, Cs.INFO_TYPE_MCH_APP)
                .eq(PayInterfaceConfig::getInfoId, mchAppId)
                .eq(PayInterfaceConfig::getIfCode, ifCodeEnum.getCode())
        );

        if (payInterfaceConfig == null) {
            return null;
        }

        return AbstractPayFactory.getInstance(ifCodeEnum).buildNormalMchParams(payInterfaceConfig.getIfParams());
    }


    public IsvsubMchParams queryIsvsubMchParams(String mchNo, String mchAppId, IfCodeEnum ifCodeEnum) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId).getIsvsubMchParamsByIfCode(ifCodeEnum);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getOne(PayInterfaceConfig.lqw()
                .select(PayInterfaceConfig::getIfCode, PayInterfaceConfig::getIfParams)
                .eq(PayInterfaceConfig::getInfoType, Cs.INFO_TYPE_MCH_APP)
                .eq(PayInterfaceConfig::getStatus, Cs.YES)
                .eq(PayInterfaceConfig::getInfoId, mchAppId)
                .eq(PayInterfaceConfig::getIfCode, ifCodeEnum.getCode())
        );

        if (payInterfaceConfig == null) {
            return null;
        }

        return AbstractPayFactory.getInstance(ifCodeEnum).buildIsvsubMchParams(payInterfaceConfig.getIfParams());
    }

    public IsvParams queryIsvParams(String isvNo, IfCodeEnum ifCodeEnum) {
        if (isCache()) {
            IsvConfigContext isvConfigContext = configContextService.getIsvConfigContext(isvNo);
            return isvConfigContext == null ? null : isvConfigContext.getIsvParamsByIfCode(ifCodeEnum);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getOne(PayInterfaceConfig.lqw()
                .select(PayInterfaceConfig::getIfCode, PayInterfaceConfig::getIfParams)
                .eq(PayInterfaceConfig::getStatus, Cs.YES)
                .eq(PayInterfaceConfig::getInfoType, Cs.INFO_TYPE_ISV)
                .eq(PayInterfaceConfig::getInfoId, isvNo)
                .eq(PayInterfaceConfig::getIfCode, ifCodeEnum.getCode())
        );

        if (payInterfaceConfig == null) {
            return null;
        }
        return AbstractPayFactory.getInstance(ifCodeEnum).buildIsvParams(payInterfaceConfig.getIfParams());

    }

    public IClientWrapper getClientWrapper(IfCodeEnum ifCodeEnum, MchAppConfigContext req) {
        if (isCache()) {
            return configContextService.getMchAppConfigContext(req.getMchNo(), req.getAppId())
                    .getClientWrapper(ifCodeEnum);
        }

        if (req.isIsvsubMch()) {
            IsvParams isvParams = queryIsvParams(req.getMchInfo().getIsvNo(), ifCodeEnum);
            return AbstractPayFactory.getInstance(ifCodeEnum).buildClientWrapper(isvParams);
        } else {
            NormalMchParams normalMchParams = queryNormalMchParams(req.getMchNo(), req.getAppId(), ifCodeEnum);
            return AbstractPayFactory.getInstance(ifCodeEnum).buildClientWrapper(normalMchParams);
        }

    }

//    public PaypalWrapper getPaypalWrapper(MchAppConfigContext mchAppConfigContext){
//        if(isCache()){
//            return
//                    configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId()).getPaypalWrapper();
//        }
//        PpPayNormalMchParams ppPayNormalMchParams = (PpPayNormalMchParams) queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.PPPAY);;
//        return PaypalWrapper.buildPaypalWrapper(ppPayNormalMchParams);
//
//    }

}
